<?php
/**
 * Created by PhpStorm.
 * User: yaolei
 * Date: 2017/11/22
 * Time: 13:17
 */

namespace Api\Controller;
use Api\Common\BaseController;

class BalanceController extends BaseController
{
    /***
     * 获取用户余额详情   用户详情也用这个
     */
    public function get(){
        $url=$_SERVER['HTTP_HOST'].__ROOT__."/";
        $user = M('user as u')->join(C("DB_PREFIX")."user_balance as ub on u.id=ub.user_id")->
        where("u.id=".$this->user_id)->
        field("u.isvalid,ub.total_balance,ub.balance,ub.use_balance,ub.frozen_balance,u.reg_time,u.id as user_id,u.username,u.name,concat('".$url."',u.headpic) as headpic,if(u.sex=1,'男','女') as sex,u.last_login_time,u.login_time,u.login_count,u.mobile,u.email")->
        find();


        if(!empty($user)){
            if($user['isvalid']==1){
                $order=M('balance_order as ord')->join(C("DB_PREFIX")."balance_goods as g on g.id=ord.balance_goods_id")->field(
                    "ord.id as order_id,ord.order_sn,g.name as goods_name,ord.price,ord.number,ord.total_price,ord.total_balance,ord.order_status,ord.payment_status,ord.payment_time,
                  ord.payment_sn,ord.createdtime,ord.payment_type"
                )->select();
                $this->response(["code"=>SUCCESS_CODE,'data'=>['user'=>$user,"order"=>$order]]);
            }else{
                $this->response(['code'=>203,'msg'=>'该用户已被冻结，请和管理员联系']);
            }
        }else{
            $this->response(['code'=>203,'msg'=>'未找到用户信息']);
        }
    }

    /***
     * 余额排行版
     * $limit 显示多少条
     * $page 页码 默认1
     */
    public function ranking(){
        $url=$_SERVER['HTTP_HOST'].__ROOT__."/";
        $limit=$this->getParams("pagesize");
        $page=$this->getParams('page');
        if(empty($limit)){
            $limit=100;
        }
        $balance = M('user_balance as ub');
        if(empty($page)){
            $page=1;
        }
        $list  = $balance->where("ub.total_balance>0 and u.isvalid=1")->join(C("DB_PREFIX")."user as u on u.id=ub.user_id")->
        field("ub.total_balance,ub.balance,ub.use_balance,ub.frozen_balance,ub.updatedtime,ub.id,ub.user_id,u.username,u.name,concat('".$url."',u.headpic) as headpic,if(u.sex=1,'男','女') as sex")->
        order('ub.total_balance desc')->page($page.',',$limit)->select();
        $count = $balance->where("ub.total_balance>0 and u.isvalid=1")->join(C("DB_PREFIX")."user as u on u.id=ub.user_id")->count();// 查询满足要求的总记录数



        if(!empty($list)){
            $this->response(['code'=>SUCCESS_CODE,'data'=>['list'=>$list,'count'=>$count]]);
        }else{
            $this->response(['code'=>203,'msg'=>'暂时没有排行信息']);
        }
    }


    public function listing(){
        $url=$_SERVER['HTTP_HOST'].__ROOT__."/";
        $limit=$this->getParams("pagesize");
        $page=$this->getParams('page');
        $sort=$this->getParams("sort");
        $sorts="";
        if(empty($limit)){
            $limit=100;
        }
        $goods = M('balance_goods as ug');
        if(empty($page)){
            $page=1;
        }
        if(empty($sort)){
            $sorts="ug.price asc";
        }else if($sort=="price desc"){
            $sorts="ug.price desc";
        }else if($sort=="buy_number asc"){
            $sorts="ug.buy_number asc";
        }else{
            $sorts="ug.buy_number desc";
        }

        $list  = $goods->where("ug.is_valid=1 AND ug.is_shelves=1")->join(C("DB_PREFIX")."user as u on u.id=ug.user_id AND u.isvalid=1","left")->
        join(C("DB_PREFIX")."user_balance as ub on u.id=ub.user_id","left")->
        field("ub.total_balance,ub.balance,ub.use_balance,ub.frozen_balance,ug.user_id,u.username,u.name,concat('".$url."',u.headpic) as headpic,if(u.sex=1,'男','女') as sex,ug.id,ug.goods_sn,ug.price,ug.description,ug.buy_number,ug.type,ug.createdtime,ug.name as goods_name,ug.balance as goods_balance")->
        order($sorts)->page($page.',',$limit)->select();
        $count = $goods->where("ug.is_valid=1 AND ug.is_shelves=1")->join(C("DB_PREFIX")."user as u on u.id=ug.user_id AND u.isvalid=1","left")->order($sorts)->count();// 查询满足要求的总记录数
        if(!empty($list)){
            $this->response(['code'=>SUCCESS_CODE,'data'=>['list'=>$list,'count'=>$count,'sorts'=>$sorts]]);
        }else{
            $this->response(['code'=>203,'msg'=>'暂时没有购买列表信息']);
        }
    }


    public function buy(){
        $id=$this->getParams("id",true);
        $url=$_SERVER['HTTP_HOST'].__ROOT__."/";
        $goods = M('balance_goods as ug');
        $model  = $goods->where("ug.is_valid=1 AND ug.is_shelves=1 AND ug.id=$id")->join(C("DB_PREFIX")."user as u on u.id=ug.user_id AND u.isvalid=1")->
        join(C("DB_PREFIX")."user_balance as ub on u.id=ub.user_id")->
        field("ub.total_balance,ub.balance,ub.use_balance,ub.frozen_balance,ug.user_id,u.username,u.name,concat('".$url."',u.headpic) as headpic,if(u.sex=1,'男','女') as sex,ug.id,ug.goods_sn,ug.price,ug.description,ug.buy_number,ug.type,ug.createdtime,ug.name as goods_name,ug.balance as goods_balance")->
        find();
        if(!empty($model)){
            $this->response(['code'=>SUCCESS_CODE,'data'=>['goods'=>$model]]);
        }else{
            $this->response(['code'=>203,'msg'=>'未找到用户购买信息']);
        }
    }
}